package at.jku.pervasive.filter;

import java.util.LinkedList;
import at.jku.pervasive.Position;

/**
 * 
 * @author hoschi14
 *
 */
public class OutlierDetection implements Filter {
	private double DISTANCE_MULTIPLICATOR = 6;
	
	@Override
	public LinkedList<Position> applyFilter(LinkedList<Position> lastPositions) {
		Position old = null;
		Position older = null;
		
		double summedDistance = 0.0;

		if (lastPositions.size() > 1)
			for (int i = lastPositions.size()-1; i > 0; i--)
			{
				double distance = 0.0;
				
				older = lastPositions.get(i);
				old = lastPositions.get(i-1);
				
				distance = Math.sqrt(Math.pow(Math.abs(older.getX() - old.getX()), 2) +  
						Math.pow(Math.abs(older.getY() - old.getY()), 2) +
						Math.pow(Math.abs(older.getZ() - old.getZ()), 2));
				
				if (i != 1)
				{
					if (summedDistance == 0)
						summedDistance += distance;
					else
						summedDistance = (summedDistance + distance) / 2;
				} else {
					if (distance > (summedDistance * DISTANCE_MULTIPLICATOR))
					{
						lastPositions.pop();
						//lastPositions.push(lastPositions.get(0));
					}
				}
			}

		return lastPositions;
	}

}
